<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<script type="text/javascript">

    $(document).ready(function() {

        $("#childLandSelected").each(function() {
            $("#childLandSelected option").attr("selected", "selected");
        });

        if (document.getElementById("childLandSelected") != null) {
            var count = document.getElementById("childLandSelected").options.length;
        }

        var mode = document.getElementById("edit_mode").value;
        if (count == 0 || mode == 0) {
            document.getElementById("childLandSelected").style.display = 'none';
            document.getElementById("deleteSelected").style.display = 'none';
        }
        $('#childLandLabel').hide();
        $('#add').hide();
        $('#lotNoId').hide();
        $('#lotNoLabel').hide();
        $('#childrenLands').hide();

    });

    //TODO: to be removed
    function showLandDetails() {
        var landIdNo = $('#landIdNo').val();
        var url = '${"displayLandDetails.do?landId=' + landIdNo + '"}';
        $.get(url, function (data) {
            $("#landDetails").html(data);
        });
    }

    function showLotNo() {
        if (document.getElementById("planNoId").value != null) {
            $('#lotNoId').show();
            $('#lotNoLabel').show();
        }
        else {
            $('#lotNoId').hide();
            $('#lotNoLabel').hide();
        }
    }
    /* function showLandMap() {
     var landIdNo = $('#landIdNo').val();
     var url = '${"displayLandDetails.do?landId=' + landIdNo + '"}';
     $.get(url, function (data) {
     $("#landDetailsMap").html(data);
     });
     }*/

    function getChildLandsWithRefNo() {
        var refNo = document.getElementById('landId').value;
        //var planNo = document.getElementById('planNoId').value;
        //var lotNo = document.getElementById('lotNoId').value;

        var planNo = 0;
        var lotNo = 0;

        $('#childLandLabel').show();
        $('#add').show();
        $('#childrenLands').show();

    <%--var url = '${"findChildrenByParentReferenceNumber.do"}';--%>
    <%--$.post(url, $('#land-detail').serialize(), function (data) {--%>
    <%--$("#childrenLands").html(data);--%>
    <%--});--%>
        var url = '${"findChildrenByParentReferenceNumber.do"}';
        $.get(url, {landReferenceNumber:refNo,planNo:planNo,lotNo:lotNo}, function (data) {
            $("#childrenLands").html(data);
        });
    }

    function getChildLandsWithPlanNo() {
        var planNo = document.getElementById('planNoId').value;

        $('#childLandLabel').show();
        $('#add').show();
        $('#childrenLands').show();

        var url = '${"findChildrenByParentReferenceNumber.do?planNo='+ planNo+ '"}';
        $.post(url, function (data) {
            $("#childrenLands").html(data);
        });
    }

    function addLands() {

        var options = "";
        var childLandReferenceNumber = $("#childLand option:selected").text();
        var childLandId = $("select#childLand").attr("value");
        var childLand = document.getElementById("childLand");
        var length = childLand.length;
        if (childLandId == 0) {
            for (i = 1; i < length; i++) {
                var val = childLand.options[i].value;
                var text = childLand.options[i].text;
                if (testDuplicate(val)) {
                    options += '<option value="' + val + '">' + text + '</option>';
                }
            }
        } else {
            if (testDuplicate(childLandId)) {
                options = '<option value="' + childLandId + '">' + childLandReferenceNumber + '</option>';
            }
        }

        document.getElementById("childLandSelected").style.display = '';
        document.getElementById("deleteSelected").style.display = '';
        $("select#childLandSelected").append(options);
        selectList();
    }


    function selectList() {
        $("#childLandSelected").each(function() {
            $("#childLandSelected option").attr("selected", "selected");
        });
    }


    function testDuplicate(val) {
        var option = document.getElementById("childLandSelected").options
        var returnVal = 0;
        if (option.length == 0) {
            returnVal = 0;
            return true;
        }
        for (var x = 0; x < option.length; x++) {
            var val1 = parseInt(option[x].value);
            var val2 = parseInt(val)
            if (val1 == val2) {
                returnVal = 1;
                return false;
            }
        }
        return true;
    }

    function removeSelectedLand() {
        var elSel = document.getElementById('childLandSelected');
        var i;
        for (i = elSel.length - 1; i >= 0; i--) {
            if (elSel.options[i].selected) {
                elSel.remove(i);
            }
        }
        var count = document.getElementById("childLandSelected").options.length;
        if (count == 0) {
            document.getElementById("childLandSelected").style.display = 'none';
            document.getElementById("deleteSelected").style.display = 'none';
        }
    }

    function searchLands() {

        var methodId = 0;

        //if (alienationTypeId == 1 || alienationTypeId == 3 || alienationTypeId == 4) {
        if ($('#alienationMethodSetId1').is(':checked')) {
            methodId = 1;
        } else if ($('#alienationMethodSetId3').is(':checked')) {
            methodId = 3;
        } else if ($('#alienationMethodSetId4').is(':checked')) {
            methodId = 4;
        } else {
            var alienationTypeId = document.getElementById('alienationTypeId').value;
            methodId = alienationTypeId;
        }

        if (methodId > 0) {
            var url = '${pageContext.request.contextPath}' + '/searchDefineAmLandsToLandAttach.do?alienationMethod=' + methodId + ' ';

            myWindow = window.open(url, '', 'width=980,height=500,scrollbars=yes');
            myWindow.focus();
        } else {
            alert("Please Select Alienation Method !");
        }

    }

</script>
<table class="width-100">
    <s:if test="initApplicationId>0">
        <s:if test="alienationType == 0 || alienationType == 1 || alienationType == 3 || alienationType == 4">
            <tr>
                <td><s:label value="Alienation Method" cssClass="mandatory-field"/></td>
                <td>
                    <s:radio id="alienationMethodSetId" list="alienationMethodSet" name="alienationType"/>
                </td>
            </tr>
        </s:if>
        <s:else>
            <s:if test="alienationType == 7">
                <s:hidden id="alienationTypeId" value="7"/>
            </s:if>
            <s:else>
                <s:hidden id="alienationTypeId" name="alienationType"/>
            </s:else>

        </s:else>
        <s:if test="editMode < 9">
            <tr>
                <td colspan="2">
                    <table>

                        <tr>
                            <td colspan="5"><h3><s:label value="%{getText('land.label')}"
                                                         cssClass="mandatory-field"/></h3></td>
                        </tr>

                        <tr>
                            <td colspan="5">
                                <fieldset>
                                    <table width="100%">
                                        <tr>
                                            <td><input type="button" id="show_landList" value="Search Lands"
                                                       onclick="searchLands()"/></td>
                                        </tr>
                                        <tr>

                                            <td><s:label value="%{getText('slmsSerialNumber.label')}"
                                                         cssClass="width-120-px"/></td>
                                            <s:if test="landReferenceNumber==0">
                                                <td colspan="3"><s:textfield name="landReferenceNumber" id="landId"
                                                                             value=""
                                                                             onKeyPress="return numbersonly(event,true)"/></td>
                                            </s:if>
                                            <s:else>
                                                <td colspan="3"><s:textfield name="landReferenceNumber" id="landId"
                                                                             onKeyPress="return numbersonly(event,true)"/></td>
                                            </s:else>

                                            <td><input type="button" id="show_children_ref_no" value="Search Land Lots"
                                                       onclick="getChildLandsWithRefNo()"/></td>


                                        </tr>
                                            <%--<tr>
                                                <td><s:label value="%{getText('plan_no.label')}"
                                                             cssClass="width-120-px"/></td>
                                                <td><s:textfield name="planNo" id="planNoId" onchange="showLotNo()"/></td>

                                                <td><s:label value="%{getText('lot_no.label')}" cssClass="width-120-px"
                                                             id="lotNoLabel"/></td>
                                                <td><s:textfield name="lotNo" id="lotNoId" value=""/></td>
                                                <td><input type="button" id="show_children_ref_no" value="Search Land Lots"
                                                           onclick="getChildLandsWithRefNo()"/></td>
                                            </tr>--%>
                                    </table>
                                </fieldset>
                            </td>
                        </tr>

                        <tr>
                                <%-- <td><s:label value="%{getText('child.label')}" id="childLandLabel"/></td>--%>
                            <td><s:label value="%{getText('land_lots.label')}" id="childLandLabel"/></td>
                            <td colspan="2">
                                <s:div id="childrenLands">

                                </s:div>
                            </td>
                            <td colspan="2"><input type="button" id="add" value="Add Land Lot" onclick="addLands()"/>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="3">
                                <s:select
                                        name="landSelected"
                                        id="childLandSelected"
                                        listKey="id"
                                        listValue="lotNumber"
                                        list="landChildList"
                                        multiple="true"/>
                            </td>
                            <td colspan="2">
                                <input type="button" id="deleteSelected" value="Delete" onclick="removeSelectedLand()"/>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </s:if>
    </s:if>
</table>
<div>
    <br>
    <%--<s:if test="editMode==1 || editMode==3 || editMode==4 || editMode==6 || editMode==11 || editMode==12 ||editMode==8 || editMode==9 || editMode==20 || editMode==28">--%>
    <%-- provide link to display land details. on click display land details below. --%>
    <%--<a href="javascript:showLandMap();">
    <s:label value="show Map Detail"/></a>
    </a>--%>
    <%--<s:include value="../../landTree.jsp"/>--%>

    <%--</s:if>--%>
    <%--   <s:if test="editMode==5">
    <s:label value="%{getText('land.label')}"/>
    <s:textfield name="landId" id="landId"/>
</s:if>    --%>
</div>
<%--
<div id="landDetailsMap" style="width: 780px;">
</div>   --%>
<%--<s:hidden id="alienationTypeId" name="alienationType"/>--%>
<s:hidden id="landIdNo" name="landApplication.alienationDetails.land.id" disabled="true"/>
